package com.dormitory.myt.sushecontroller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.dormitory.myt.common.R;
import com.dormitory.myt.en.Allsushe;
import com.dormitory.myt.en.Detail;
import com.dormitory.myt.en.Xsxs;
import com.dormitory.myt.service.AllsusheService;
import com.dormitory.myt.service.DetailService;
import com.dormitory.myt.service.XsxsService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;
@Slf4j
@RestController
@RequestMapping ("/xsxs")
public class XsxsController {


    @Autowired
    private XsxsService xsxsService;
    @Autowired
    private AllsusheService allsusheService;
    @Autowired
    private DetailService detailService;

    /**
     * 插入可选宿舍
     * @param response
     * @param zy
     * @param sex
     * @param sushe
     * @return
     */
    @GetMapping("/xsxs1")
    public R<String> xsxs(HttpServletResponse response,@RequestParam String zy, @RequestParam String sex, @RequestParam String sushe){  //@RequestParam(value = "sushe" ) List<Integer> suhse
        response.setHeader("Access-Control-Allow-Origin","*");
        //判断是否以及存在:
        LambdaQueryWrapper<Xsxs> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Xsxs::getZy,zy).eq(Xsxs::getSex,sex);

        if(!xsxsService.list(queryWrapper).isEmpty()){
            log.info("对象不为空，说明已经有了，返回");
            return R.error("选项以及存在");
        }
        log.info("等于空.插入数据");
        Xsxs xsxs=new Xsxs();
        xsxs.setZy(zy);
        xsxs.setSex(sex);
        xsxs.setSushe(sushe);
        xsxsService.save(xsxs);  //可能一个专业有多个数据的风险，bug！


    return R.success("year");
    }

    /**
     * 获取可选宿舍：
     * @param response
     * @param zy
     * @param sex
     * @return
     */
    @GetMapping("/xsxs2")
    public  R<Xsxs> xsxs2(HttpServletResponse response,@RequestParam String zy, @RequestParam String sex){
        response.setHeader("Access-Control-Allow-Origin","*");
        Xsxs xsxs=new Xsxs();
        xsxs.setZy(zy);
        xsxs.setSex(sex);
        System.out.println(xsxs);
        //条件构造器:
        LambdaQueryWrapper<Xsxs> queryWrapper = new LambdaQueryWrapper<>();
        //根据专业和性别来查询：
        queryWrapper.eq(xsxs.getZy() != null,Xsxs::getSex,xsxs.getSex()).eq(Xsxs::getZy,xsxs.getZy());

        List<Xsxs> list = xsxsService.list(queryWrapper);
        Xsxs firstXsxs = list.get(0);

        return R.success(firstXsxs);
    }


    /**
     * 查宿舍号表
     * @param response
     * @param dong
     * @param lou
     * @return
     */
    @GetMapping("/susheHao")
public R<List<Allsushe>> xsxs3(HttpServletResponse response,@RequestParam String dong, @RequestParam String lou){
    response.setHeader("Access-Control-Allow-Origin","*");
    Allsushe allsushe=new Allsushe();
    allsushe.setDong(dong);
    allsushe.setLou(lou);
    //条件构造器:
    LambdaQueryWrapper<Allsushe> queryWrapper = new LambdaQueryWrapper<>();
    //根据楼栋来查询：
    queryWrapper.eq(Allsushe::getLou,allsushe.getLou()).eq(Allsushe::getDong,allsushe.getDong());

    List<Allsushe> list = allsusheService.list(queryWrapper);

    return R.success(list);
}


    /**
     * 查宿舍床位：
     * @param response
     * @param sushehao
     * @return
     */
    @GetMapping("/bed")
    public R<List<Detail>> xsxs4(HttpServletResponse response, @RequestParam Integer sushehao){
        response.setHeader("Access-Control-Allow-Origin","*");
        Detail detail=new Detail();
        detail.setSushehao(sushehao);

        //条件构造器:
        LambdaQueryWrapper<Detail> queryWrapper = new LambdaQueryWrapper<>();
        //根据studentid为空 来查询：
        queryWrapper.isNull(Detail::getStudentid).eq(Detail::getSushehao,detail.getSushehao());

        List<Detail> list = detailService.list(queryWrapper);

        return R.success(list);
    }

    /**
     * 确定选择：
     * 并将总宿舍表的对应宿舍 人数number字段加一
     * 向详情表绑定studentid
     * @param response
     * @param sushehao
     * @param studentid
     * @param bed
     * @return
     */
    @GetMapping("/submit")
    public R<String> xsxs5( HttpServletResponse response,@RequestParam Integer sushehao,@RequestParam String studentid, @RequestParam Integer bed){
        response.setHeader("Access-Control-Allow-Origin","*");
       // 1.向详情表绑定studentid:
        Detail detail=new Detail();
        detail.setSushehao(sushehao);
        //去掉双引号：
        studentid = studentid.replace("\"", "");
        detail.setStudentid(studentid);
        detail.setBed(bed);
        detailService.updateStudentid(detail);

        //2.更新总宿舍表的对应宿舍 人数number字段加一:

        allsusheService.updateNumber(sushehao);

        return R.success("year");
    }
}

